<?php


namespace App\Model;


use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

/**
 * Class ConfigSystemModel
 * @package App\Model
 * @property $type
 * @property $name
 * @property $value
 */
class ConfigSystemModel extends AbstractModel
{
    protected $tableName = 't_config_system';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception|\Throwable
     */
    public function getConfig($where)
    {
        return $this->get($where);
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('系统配置表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colVarChar('type', 20)->setDefaultValue('')->setIsNotNull()->setColumnComment('类型');
        $table->colVarChar('name', 100)->setDefaultValue('')->setIsNotNull()->setColumnComment('配置名');
        $table->colVarChar('value', 256)->setDefaultValue('')->setIsNotNull()->setColumnComment('配置值');
        $table->colVarChar('description', 256)->setDefaultValue('')->setIsNotNull()->setColumnComment('描述');

        $table->indexNormal('type_index', 'type');
        $table->indexNormal('name_index', 'name');
        $table->indexNormal('value_index', 'value');

        return $table;
    }
}